package com.hdcircles.autoform.model.http;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.hdcircles.autoform.app.App;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by hdcircles on 2016/10/25.
 */

public class LocationHelper {

    private AMapLocationClient locationClient;

    private MapLocationListener locationListener;

    private static List<HashMap<String,String>> locationList;

    public LocationHelper(Context context){

        locationClient=new AMapLocationClient(context);

        //设置定位参数
        locationClient.setLocationOption(getDefaultOption());

        locationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                locationListener.onLocationChanged(aMapLocation);
            }
        });
    }

    public void startLocation(){
        locationClient.startLocation();
    }

    public void setLocationListener(MapLocationListener locationListener){
        this.locationListener=locationListener;
    }

    public interface MapLocationListener{
        void onLocationChanged(AMapLocation location);
    }




    /**
     * 默认的定位参数
     * @return
     */
    private AMapLocationClientOption getDefaultOption(){
        AMapLocationClientOption option=new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//设置定位模式，可选的模式有高精度、仅设备、仅网络。默认高精度
        option.setGpsFirst(true);//可选，设置是否gps优先，只在高精度模式下有效。
        option.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒，在仅设备模式下无效
        option.setInterval(2000);//可选，设置定位间隔。默认为2秒
        option.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认true
        option.setOnceLocation(false);//可选，设置是否单次定位。默认false
//        option.setOnceLocationLatest(false);
        return option;
    }

    public void stopLocation(){
        locationClient.stopLocation();
    }

    public void destroyLocation(){
        if (null!=locationClient){
            locationClient.onDestroy();
//            locationClient=null;
        }
    }

    public  List<HashMap<String,String>> searchLocation(String msg){
        InputtipsQuery inputtipsQuery=new InputtipsQuery(msg,"全国");
        inputtipsQuery.setCityLimit(true);
        Inputtips inputtips=new Inputtips(App.getInstance(),inputtipsQuery);
        inputtips.setInputtipsListener(new Inputtips.InputtipsListener() {
            @Override
            public void onGetInputtips(List<Tip> list, int rCode) {
                if (rCode==1000){
                    List<HashMap<String,String>> listString=new ArrayList<HashMap<String, String>>();
                    for (int i=0;i<list.size();i++){
                        HashMap<String,String> map=new HashMap<String, String>();
                        map.put("name",list.get(i).getName());
                        map.put("address",list.get(i).getAddress());
                        listString.add(map);
                    }
                    locationList=listString;
                }
            }
        });
        inputtips.requestInputtipsAsyn();
        return locationList;
    }

}
